package com.song.common.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResponse<T> {

    @ApiModelProperty("相应错误编码，0为正常")
    private int code;

    @ApiModelProperty("相应错误信息")
    private String message;

    @ApiModelProperty("响应内容")
    private T result;

    public static <T> RestResponse<T> success() {
        return new RestResponse<T>();
    }

    public static <T> RestResponse<T> success(T result) {
        RestResponse<T> response = new RestResponse<>();
        response.setResult(result);
        return response;
    }

    public static <T> RestResponse<T> error(String message) {
        RestResponse<T> response = new RestResponse<>();
        response.setCode(-2);
        response.setMessage(message);
        return response;
    }

    public RestResponse() {
        this(0, "");
    }

    public RestResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @JsonIgnore
    public Boolean isSuccessful() {
        return this.code == 0;
    }

    @Override
    public String toString() {
        return "RestResponse [code=" + code + ", message=" + message + ", result=" + result + "]";
    }

}
